<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>品牌管理</title>
    <link rel="stylesheet" href="../css/elementui.css">
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>

</head>
<body>

<div id="app">
    <el-form :inline="true">
        <el-form-item label="品牌名称">
            <el-input placeholder="品牌名称" v-model="searchMap.name"></el-input>
            <!--v-model="searchMap.name获取名字-->
        </el-form-item>
        <el-form-item label="品牌首字母">
            <el-input placeholder="品牌首字母" v-model="searchMap.letter"></el-input>
            <!--v-model="searchMap.letter"获取首字母-->
        </el-form-item>
        <el-button type="primary" @click="fetchData">查询</el-button>
        <el-button type="primary" @click="pojo={},formVisible = true">新增</el-button>
    </el-form>
    <el-table
            :data="tableData"
            border
            style="width: 100%">
        <el-table-column
                prop="id"
                label="ID"
                width="180">
        </el-table-column>
        <el-table-column
                prop="name"
                label="名称"

        >
        </el-table-column>
        <el-table-column
                prop="letter"
                label="首字母"
                width="180">
        </el-table-column>
        <el-table-column
                label="图片"
                width="180">
            <template slot-scope="scope">
                <img :src="scope.row.image">
            </template>
        </el-table-column>
        <el-table-column
                label="操作"
                width="180"
        >
            <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" @click="edit(scope.row.id)" size="small"></el-button>
                <el-button type="primary" icon="el-icon-delete" size="small"@click="dele(scope.row.id)"></el-button>
            </template>
        </el-table-column>
    </el-table>

    <el-pagination
            @size-change="fetchData"
            @current-change="fetchData"
            :current-page.sync="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>


    <el-dialog
            title="品牌编辑"
            :visible.sync="formVisible">
       <!--表单-->
        <el-form label-width="80px">
            <el-form-item label="品牌名称">
                <el-input placeholder="品牌名称" v-model="pojo.name"></el-input>
                <!--v-model="pojo.name获取名字-->
            </el-form-item>
            <el-form-item label="品牌首字母">
                <el-input placeholder="品牌首字母" v-model="pojo.letter"></el-input>
                <!--v-model="pojo.letter"获取首字母-->
            </el-form-item>
            <el-form-item label="品牌图片">
                <el-upload
                        class="avatar-uploader"
                        action="http://localhost:9101/upload/native.do"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>

                <!--v-model="pojo.image"品牌图片-->
            </el-form-item>
            <el-form-item label="品牌排序">
                <el-input placeholder="品牌排序" v-model="pojo.seq"></el-input>
                <!--v-model="pojo.seq"品牌排序-->
            </el-form-item>
            <el-form-item>
                <!--按钮-->
                <el-button @click="formVisible = false">关闭</el-button>
                <el-button @click="save()">保存</el-button>
            </el-form-item>


        </el-form>




    </el-dialog>


</div>
</body>
<script src="../js/vue.js"></script>
<script src="../js/elementui.js"></script>
<script src="../js/axios.js"></script>

<script>
    new Vue({
        el: "#app",
        data() {
            return {
                tableData: [],
                currentPage: 1,
                size: 10,
                total: 10,
                searchMap: {},//多条件查询
                formVisible: false,
                pojo:{},
                imageUrl:''//空字符串

            }
        },
        created() {
            this.fetchData()
        },
        methods: {
            fetchData() {
                axios.post(`/brand/findPage.do?page=${this.currentPage}&size=${this.size}`, this.searchMap).then(response => {
                    this.tableData = response.data.rows;
                    this.total = response.data.total;
                })
            },
            save(){
                this.pojo.image=this.imageUrl;//保存时把存入的图片地址付给数据库中的image
                axios.post(`/brand/${this.pojo.id==null?'add':'update'}.do`,this.pojo).then(respone=>{
                    this.formVisible=false;//窗口关闭
                    this.fetchData();//刷新数据
                })
            },
            edit(id){
                this.formVisible=true;//把修改窗口打开
                //把数据显示在表单里，单传id
                axios.get(`/brand/findById.do?id=${id}`).then(response=>{
                    this.pojo=response.data
                    this.imageUrl=this.pojo.image//取的时候把数据库中的地址付给imageUrl

                })
            },
            dele(id){
                this.$confirm('此操作将永久删除id为'+id+'的文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //删除操作
                    axios.get(`/brand/delete.do?id=${id}`).then(response=>{
                        this.fetchData();
                    })
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {

                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = file.response;//file.response上传文件返回的具体物理地址
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },

        }


    })
</script>


</html>